package strcy.cn.market.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import strcy.cn.market.mapper.ProductMapper;
import strcy.cn.market.pojo.Product;
import strcy.cn.market.pojo.VO.ProductVO;
import strcy.cn.market.service.ProductService;
import strcy.cn.market.utils.PageBean;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void add(Product product) {
        productMapper.add(product);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public List<ProductVO> list() {
        List<ProductVO> list = productMapper.list();
        return list;
    }

    @Override
    public void updateStatus(String status, Integer id) {
        productMapper.updateStatus(status,id);
    }

    @Override
    public PageBean<ProductVO> pageList(Integer pageNum, Integer pageSize) {
        //创建pagebean对象
        PageBean<ProductVO> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper
        List<ProductVO> as =productMapper.list();
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录数和当前页数据
        Page<ProductVO> p = (Page<ProductVO>) as;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public ProductVO getById(Integer productId) {
        ProductVO p = productMapper.getById(productId);
        return p;
    }
}
